<?php
require_once("db_functions.php");
require_once("rokovnik.class.php");


class ugovori_fox extends rokovnik{
	
	var $foxdb;
	var $db;
	var $komid;
	
	var $oldserverpath = "http://cvet.yellow/yap1/";
	
	function ugovori_fox($komid){
		require_once($this->oldserverpath."classes/adodb_vfp.php");
		$this->db = new db_functions();
		$this->foxdb = $db;
		$this->komid = $komid;
	}
	
	function get(){
		
		$ret = false;
		$komid = $this->checkSifra($this->komid);
		$q = "SELECT ugovor.ugoukcen, ugovor.ugodug, ugovor.ugoid, ugovor.ugodat, ugovor.ugostorno, ugostv.ugscart, ugostv.ugskol, art.artnaz, ugostv.delid, delatnos.delnaz, radnici.radime, radnici.radprez "
			."FROM ugovor ,ugostv ,art ,radnici ,delatnos "
			."WHERE ugovor.ugoid = ugostv.ugoid "
			."AND ugostv.artid = art.artid "
			."AND ugostv.delid = delatnos.delid "
			."AND Ugovor.radid = Radnici.radid "
			."AND Ugovor.parid = $komid "
			."ORDER BY ugovor.ugodat DESC, ugostv.ugscart DESC, art.artnaz";
		
		$res = $this->foxdb->Execute($q);
		$num = $res->_numOfRows;
		//echo $q;
		if($num){
			$ret = array();
			while (!$res->EOF) {
				foreach ($res->fields as $k=>$f){
					$f = latins($f);
					$res->fields[$k] = trim($f);
				}
				$ugoid = $res->fields["ugoid"];
				
				$tmp = explode("-",trim($res->fields["ugodat"]),3);
				$datum = $tmp[2].".".$tmp[1].".".$tmp[0];
				$cena  = number_format($res->fields["ugoukcen"],2,",",".");
				$dugov = number_format($res->fields["ugodug"],2,",",".");
				$storn = $res->fields["ugostorno"];
				$artik = $res->fields["artnaz"];
				$artce = number_format($res->fields["ugscart"],2,",",".");
				$kolic = $res->fields["ugskol"];
				$delid = $res->fields["delid"];
				$delat = $res->fields["delnaz"];
				$radni = $res->fields["radime"]." ".$res->fields["radprez"];
				
				$ret[$ugoid]["id"]		= $ugoid;
				$ret[$ugoid]["radnik"]	= $radni;
				$ret[$ugoid]["datum"]	= $datum;
				$ret[$ugoid]["cena"]	= $cena;
				$ret[$ugoid]["dug"]		= $dugov;
				$ret[$ugoid]["storno"]	= $storn;
				$ret[$ugoid]["delnaz"]	= $delat;
				$ret[$ugoid]["delid"]	= $delid;
				
				$ret["$ugoid"]["artikl"][] = array("name"=>$artik,"price"=>$artce);
				
				$res->MoveNext();
			}
		}
		return $ret;
	}
	function searchUgovori($kword){
		
		$polja = array(
			"ugovor.ugoparzr"=>"string",
			"partner.parnaz"=>"string",
			"ugovor.ugoparpd"=>"string",
			"ugovor.ugoid"=>"string",
			"ugovor.radid"=>"integer"
		);
		
		$q = "SELECT ugovor.ugoukcen, ugovor.ugodug, ugovor.ugoid, ugovor.ugodat, ugovor.ugostorno, ugostv.ugscart, ugostv.ugskol, art.artnaz, delatnos.delnaz, radnici.radime, radnici.radprez, "
			."partner.parid, partner.parnaz, partner.partel, ugovor.ugoparzr, ugovor.ugoparpd, kampanja.kamnaz, ugovor.ugonap, ugostv.ugspotne, ugostv.ugsurad, ugostv.delid, ugostv.ugsprofil, ugostv.ugsprofile "
			."FROM ugovor ,ugostv ,art ,radnici ,delatnos, partner, kampanja "
			."WHERE ugovor.ugoid = ugostv.ugoid "
			."AND ugovor.kamid=kampanja.kamid "
			."AND partner.parid=ugovor.parid "
			."AND ugostv.artid = art.artid "
			."AND ugostv.delid = delatnos.delid "
			."AND Ugovor.radid = Radnici.radid AND (";
		
		//$kword_latin = latins($kword,true);
		$kword_latin = $kword;
		foreach ($polja as $fld=>$type){
			if($type == "string"){
				//$skw = mb_convert_case($kword_latin,1,"WINDOWS-1250");
				//$skw = mb_convert_case($kword_latin,MB_CASE_UPPER,"UTF-8");
				$skw = preg_replace('/[čČćĆšŠđĐžŽ]/','%',strtoupper($kword));
				$q .= "UPPER($fld) LIKE '%$skw%' OR ";
			}else{
				$kword = intval($kword);
				if($kword>0){
					$q .= "$fld=$kword OR ";
				}
			}
		}
			
		$q = substr($q,0,strlen($q)-4).") ORDER BY partner.parnaz ASC, ugovor.ugodat DESC, ugostv.ugscart DESC, art.artnaz";
		//echo "$q<br />".mb_detect_encoding($q);
		$res = $this->foxdb->Execute($q);
		if($res->_numOfRows>0){
			while (!$res->EOF) {
				foreach ($res->fields as $name=>$value){
					$value = latins(trim($value));
					$ret[$name][] = $value;
				}
				$res->MoveNext();
			}
			return $ret;
		}else{
			return false;
		}
	}
	
	
	function getPlanOtplate($brugo){
		$q = "SELECT ugpdat, ugpbruto FROM ugopotp WHERE ugoid='$brugo' ORDER BY ugpdat DESC";
		$res = $this->foxdb->Execute($q);
		if($res->_numOfRows>0){
			while (!$res->EOF) {
				$ret[] = array(
					"datum" => datetime2str(trim($res->fields["ugpdat"])),
					"iznos" => trim($res->fields["ugpbruto"])
				);
				$res->MoveNext();
			}
			return $ret;
		}else{
			return false;
		}
	}
	function getOtplata($brugo){
		$q = "SELECT uodatum, uoiznos FROM ugootp WHERE ugoid='$brugo' ORDER BY uodatum DESC";
		$res = $this->foxdb->Execute($q);
		if($res->_numOfRows>0){
			while (!$res->EOF) {
				$ret[] = array(
					"datum" => datetime2str(trim($res->fields["uodatum"])),
					"iznos" => trim($res->fields["uoiznos"])
				);
				$res->MoveNext();
			}
			return $ret;
		}else{
			return false;
		}
	}
	
	
	function importKomitentWWW(){
		$delid = getFromInput("delid");
		
		$foxid = $this->db->getOne("SELECT fox FROM yapfox WHERE yap=$this->komid LIMIT 1");
		if($foxid !== null){
			$komid = $foxid;
		}else{
			$komid = $this->komid;
		}
		
		
		$q = "SELECT ugovor. ugodat, ugovor.ugoid, ugostv.artid, art.artnaz, ugostv.ugsnaziv, ugostv.ugsdpos, ugostv.ugsistek, ugostv.ugsdpzh, ugostv.ugswats, ugostv.ugswaks "
			."FROM ugovor INNER JOIN ugostv INNER JOIN art ON ugostv.artid=art.artid ON ugovor.ugoid=ugostv.ugoid "
			."WHERE parid=$komid AND ugostv.delid=$delid ORDER BY ugovor.ugodat DESC";
		//echo $q;
		$res = $this->foxdb->Execute($q);
		$data = array();
		if($res->_numOfRows>0){
			while (!$res->EOF) {
				foreach ($res->fields as $k=>$v){
					$v = latins($v);
					$$k = trim($v);
				}
				
				$data[$ugoid][$artid][] = array(
					"datum"		=> $ugodat,
					"artikl"	=> $artnaz,
					"naziv"		=> $ugsnaziv,
					"start"		=> $ugsdpos,
					"end"		=> $ugsistek,
					"hosting"	=> $ugsdpzh,
					"test"		=> $ugswats,
					"final"		=> $ugswaks
				);
				$res->MoveNext();
			}
			//print_rarray($data);
			
			foreach ($data as $ugoid=>$v){
				$date = $artikli = $logo = $logo_start = $logo_end = $baner = $baner_start = $baner_end = $host = $test = $final = "";
				foreach ($v as $artid=>$m){
					foreach ($m as $n){
						$artikli .= $n["artikl"]." &middot; ";
						
						if(strlen($n["naziv"])>0){
							if(preg_match('/^\/logo\//',$n["naziv"])){
								$logo = $n["naziv"];
								$logo_start = date("d.m.Y",date2int($n["start"],"-",array("day"=>2,"month"=>1,"year"=>0)));
								$logo_end = date("d.m.Y",date2int($n["end"],"-",array("day"=>2,"month"=>1,"year"=>0)));
							}else{
								$baner= $n["naziv"];
								$baner_start = date("d.m.Y",date2int($n["start"],"-",array("day"=>2,"month"=>1,"year"=>0)));
								$baner_end = date("d.m.Y",date2int($n["end"],"-",array("day"=>2,"month"=>1,"year"=>0)));
							}
						}
						$di = date2int($n["hosting"],"-",array("day"=>2,"month"=>1,"year"=>0));
						if($di>0){
							$host = date("d.m.Y",$di);
						}
						if(strlen($n["test"])>0){
							$test = $n["test"];
						}
						if(strlen($n["final"])>0){
							$final = $n["final"];
						}
						$di = date2int($n["datum"],"-",array("day"=>2,"month"=>1,"year"=>0));
						if($di>0){
							$date = date("d.m.Y",$di);
						}
					}
				}
				$artikli = substr($artikli,0,strlen($artikli)-10);
				$title[]		= "$ugoid - $date<br />$artikli";
				$logo1[]			= $logo;
				$logo_start1[]	= $logo_start;
				$logo_end1[]		= $logo_end;
				$baner1[]		= $baner;
				$baner_start1[]	= $baner_start;
				$baner_end1[]	= $baner_end;
				$host1[]			= $host;
				$test1[]			= $test;
				$final1[]		= $final;
			}
			
			// UKUPNO
			$this->assign("ulogo_name",$this->getFirstNonNullValue($logo1));
			$this->assign("ulogo_start",$this->getFirstNonNullValue($logo_start1));
			$this->assign("ulogo_end",$this->getFirstNonNullValue($logo_end1));
			$this->assign("ubaner_name",$this->getFirstNonNullValue($baner1));
			$this->assign("ubaner_start",$this->getFirstNonNullValue($baner_start1));
			$this->assign("ubaner_end",$this->getFirstNonNullValue($baner_end1));
			$this->assign("uhosting",$this->getFirstNonNullValue($host1));
			$this->assign("utest_adresa",$this->getFirstNonNullValue($test1));
			$this->assign("ukonac_adresa",$this->getFirstNonNullValue($final1));
			
			$this->assign("title",$title);
			$this->assign("logo_name",$logo1);
			$this->assign("logo_start",$logo_start1);
			$this->assign("logo_end",$logo_end1);
			$this->assign("baner_name",$baner1);
			$this->assign("baner_start",$baner_start1);
			$this->assign("baner_end",$baner_end1);
			$this->assign("hosting",$host1);
			$this->assign("test_adresa",$test1);
			$this->assign("konac_adresa",$final1);
			
			$this->assign("objid",getFromInput("objid"));
			$template = $this->parseTemplate("komitenti/www_data_import.tpl");
			//file_put_contents("tmpvoja.tmp",$template);
			echo $template;
		}else{
			echo "<div class=\"area\">Nema ugovora.</div>";
		}
	}
	function getFirstNonNullValue($niz){
		$ret = "";
		foreach ($niz as $val){
			if(strlen($val)>0){
				$ret = $val;
				break;
			}
		}
		return $ret;
	}
	
	function checkSifra($komid){
		$q = "SELECT fox FROM yapfox WHERE yap=$komid";
		$fox = $this->db->getOne($q);
		if($fox === null){
			return $komid;
		}else{
			return $fox;
		}
	}
	
}


function latins($str,$reverse=false){
	
	$str = mb_convert_encoding($str,"UTF-8");
	
	$mask = array(
		"č" => chr(195).chr(168),
		"Č" => chr(195).chr(136),
		"ć" => chr(195).chr(166),
		"Ć" => chr(195).chr(134),
		"š" => chr(194).chr(154),
		"Š" => chr(194).chr(138),
		"đ" => chr(195).chr(176),
		"Đ" => chr(195).chr(144),
		"ž" => chr(194).chr(158),
		"Ž" => chr(194).chr(142)
	);
	$mask = array(
		"č" => chr(232),
		"Č" => chr(200),
		"ć" => chr(230),
		"Ć" => chr(198),
		"š" => chr(154),
		"Š" => chr(138),
		"đ" => chr(240),
		"Đ" => chr(208),
		"ž" => chr(158),
		"Ž" => chr(142)
	);
	
	foreach ($mask as $k=>$v){
		if($reverse){
			$str = str_replace($k,$v,$str);
		}else{
			$str = str_replace($v,$k,$str);
		}
	}
	return $str;
}

?>